package eerbaar.client.internal.ctr.pet;

import eerbaar.client.ctr.IPersonaCmd;
import eerbaar.client.exp.ExpException;
import eerbaar.client.exp.IExp;
import eerbaar.client.exp.IScope;
import eerbaar.client.exp.ScopeManager;
import eerbaar.client.internal.ctr.operator.area.ValueArea;
import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.position.IPosition;
import eerbaar.position.WrongPositionException;

public class EvaluationAdaptor implements IPersonaCmd {

    private final IExp exp;

    public EvaluationAdaptor(IExp exp) {
        this.exp = exp;
    }

    @Override
    public IPosition calcDestination(IGame game, IPersona persona)
            throws WrongPositionException {

        IScope scope = new ScopeManager().getRoot();
        scope.setTarget(persona);
        scope.setGame(game);

        return (IPosition) this.exp.calcDestination(scope);
    }

    @Override
    public boolean execute(IGame game, IPersona persona)
            throws WrongPositionException {

        boolean result = true;

        IScope scope = new ScopeManager().getRoot();
        scope.setTarget(persona);
        scope.setGame(game);
        Object va = scope.getValueArea();
        if (va == null) {
            scope.setValueArea(new ValueArea(game));
        }

        try {

            scope = this.exp.interpret(scope);

            result = scope.getState().isFinished();

        } catch (ExpException e) {

            System.out.println(e.getWord());
        }

        return result;
    }

    @Override
    public String toString() {

        return "personaCmd(" + this.exp + ")";
    }

}
